package id.co.rho.scoring.cid.client.mvc.errorhandling;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.MessageBox;

import id.co.rho.scoring.cid.client.mvc.AppEvents;

/**
 * Created by IntelliJ IDEA.
 * User: bungrudi
 * Date: 8/10/11
 * Time: 9:30 AM
 */
public class ErrorHandlingView extends View {
    private MessageBox messageBox;

    public ErrorHandlingView() {
        super(null);
        this.controller = new ViewController();
    }

    @Override
    protected void initialize() {
        super.initialize();
        messageBox = new MessageBox();
        messageBox.setClosable(true);
        messageBox.setType(MessageBox.MessageBoxType.ALERT);
        // TODO graceful error message
        messageBox.setTitle("Error TODO: find a graceful error message");
    }

    @Override
    protected void handleEvent(AppEvent event) {
        messageBox.setMessage(event.<Throwable>getData().getMessage());
        messageBox.show();
    }

    private class ViewController extends Controller {
        private ViewController() {
            registerEventTypes(AppEvents.ExceptionCaught);
        }

        @Override
        public void handleEvent(AppEvent event) {
            forwardToView(ErrorHandlingView.this,event);

        }
    }
}
